/*
 * Created on 14-sep-2005
 *
 */
package com.arethusa;
import java.io.FileInputStream;
import java.util.Properties;

import simpleorm.core.SException;
import simpleorm.core.SLog;


/**
 * @author tpijl
 *
 */
public class LangebaanDB {

	/**
	 * Simple property mechanism, reads from the ~/simpleotm.properties file.
	 * Used by Default configuration to set the database.name etc.
	 */
	static public class SystemProperties {

	/**
	 * Causes the property cache to be emptied so that properties are re-read from the
	 * property file next time a property is required.
	 */
		public void refreshProperties() {
			properties = null;
		}
		protected Properties properties;
		
		protected void loadProperties() {
			if (properties == null){
				String propsFile = null;
				try {
					String home = System.getProperty("user.home");
					propsFile = home + "/simpleorm.properties";
					SLog.slog.connections("Loading properties from " + propsFile);
					properties = new Properties(System.getProperties());
					FileInputStream in = new FileInputStream(propsFile);
					properties.load(in);
					in.close();		
				} catch (Exception ex) {
					throw new SException.Error("While obtaining Properties from " + propsFile, ex);
				}
			}
		}
		
		public String getProperty(String property) {
			loadProperties();
			String prop = properties.getProperty(property);
			return prop==null?null:prop.trim();
			// The .trim() is important!  
			// I once spent a long time tacking down a trailing space in a properties file driver name!
		}
		public String getProperty(String property, String defalt) {
			String val = getProperty(property);
			return val==null?defalt:val;
		}
		public void setProperty(String property, String value) {
			loadProperties();
			properties.setProperty(property, value);
		}
	}

}
